package com.powernode.springmvc.controller.springmvc.controller;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

//在类上标注 @Controller 注解，纳入IoC容器的管理。
//        当然，也可以采用 @Component注解进行标注。
//        @Controller 只是 @Component 注解的别名。
@Controller
public class IndexController {
    @RequestMapping("/")
    public String index(){
        return "index";
    }

    @RequestMapping("/first")
    public String first(){
        //处理业务逻辑
        System.out.println("业务正在处理中");

        //返回一个逻辑视图名称
        return "first";
    }

//    @RequestMapping(value = {"/testVal1", "/testVal2"})
//    @RequestMapping(path = {"/testVal1", "/testVal2"})
    @RequestMapping({"/testVal1", "/testVal2"})  //这三种写法都行
    //path是value的别名，也可以使用
    public String testRequestMappingValue(){
        return "test_value";
    }

    //Ant风格的注解：支持模糊匹配
    @RequestMapping("/x?z/testAntValue")
//    @RequestMapping("/x*z/testAntValue")
//    @RequestMapping("/x**z/testAntValue")
    public String testRequestMappingAntValue(){
        return "ok";
    }

    @RequestMapping(value = "/login/{username}/{password}")
//    @RequestMapping(value = "/login/{a}/{b}")
    public String testRESTFulURL(
            //@PathVariable("a")
            @PathVariable("username")
            String username,
            @PathVariable("password")   //将访问路径上的username传给变量username
            String password){
        //http://192.168.108.1:8080/springmvc02/login/zhangsan/1234
        //用户名：zhangsan密码：1234
        System.out.println("用户名：" + username + "密码：" + password);
        return "ok";
    }

    //当前端发送的请求路径是 /user/login ，并且发送的请求方式是以POST方式请求的。则可以正常映射。
    //两个条件都满足才能映射
    @RequestMapping(value = "/user/login",method = {RequestMethod.POST,RequestMethod.GET})
    //衍生Mapping
    //@PostMapping("/user/login")
    public String userLogin(){
        System.out.println("处理登录业务中...");
        return "ok";
    }

      @RequestMapping(value = "/testParams",params = {"username","password"})
//    @RequestMapping(value = "/user/login",params = {"!username","password"})
//    @RequestMapping(value = "/user/login",params = {"username=admin","password"})
//    @RequestMapping(value = "/user/login",params = {"username!=admin","password"})
    public String testParams(){
        return "ok";
    }


    @RequestMapping(value = "/testHeaders",headers = "Referer=http://192.168.108.1:8080/springmvc02/" )
//    @RequestMapping(value = "/testHeaders",headers = {"Referer","Host"})
    public String testHeaders(){
        return "ok";
    }
}
